DevForce Help Reference
Start(Func<IEnumerable<INotifyCompleted>>,Action<CoroutineOperation>) Method
Example 


An iterator block containing asynchronous actions
Optional completion handler
Start serial execution of multiple asynchronous actions.
Syntax
'Declaration
 
Public Overloads Shared Function Start( _
   ByVal coroutine As Func(Of IEnumerable(Of INotifyCompleted)), _
   Optional ByVal completedHandler As Action(Of CoroutineOperation) _
) As CoroutineOperation
'Usage
 
Dim coroutine As Func(Of IEnumerable(Of INotifyCompleted))
Dim completedHandler As Action(Of CoroutineOperation)
Dim value As CoroutineOperation
 
value = Coroutine.Start(coroutine, completedHandler)

Parameters

coroutine
An iterator block containing asynchronous actions
completedHandler
Optional completion handler

Return Value

A CoroutineOperation representing this operation
Remarks
Used to start serial execution of the asynchronous, and synchronous, actions within the specified iterator block. Asychronous actions within the block will cause subsequent actions to "wait" until each asynchronous operation yields when complete.

See the second example below for how to pass arguments into the iterator.

Example
public void CoroutineSample() {
   // Note that a new EM is not required for a Coroutine, we show it here for completeness.
   _mgr = new DomainModelEntityManager();
   
   // Start some serial async operations.
   var op = Coroutine.Start(SampleActions);
   
   // Listen for completion.
   op.Completed += (s, e) => {
     if (e.HasError) {
       MessageBox.Show(e.Error.Message);
       e.MarkErrorAsHandled();
     } 
   };
 }
 
 private EntityManager _mgr;

 // A block of asynchronous actions.
 private IEnumerable<INotifyCompleted> SampleActions() {

   // Start a query for all customers in specified country, yield when async op completes.
   var op1 = _mgr.Customers.Where(c => c.Country == "UK").ExecuteAsync();
   yield return op1;

   // Resume execution here when op1 completes.  Take a look at results from 1st query
   TraceFns.WriteLine("Customer count = " + op1.Results.Count().ToString());

   // Perform another async quuery for all employees.
   var op2 = _mgr.Employees.ExecuteAsync();
   yield return op2;

   // Resume execution here when op2 completes.  See what it returned.
   TraceFns.WriteLine("Employee count = " + op2.Results.Count().ToString());
 }    
 
 /***************************************************************************************/
 // Sample 2 - passing arguments to an iterator
 
 public void CoroutineSample2a() {
   // Note that a new EM is not required for a Coroutine, we show it here for completeness.
   _mgr = new DomainModelEntityManager();

   // Start some serial async operations.
   var op = Coroutine.Start(() => SampleActions2("USA"));
   
   // Listen for completion.
   op.Completed += (s, e) => {
     if (e.HasError) {
       MessageBox.Show(e.Error.Message);
       e.MarkErrorAsHandled();
     } 
   };
 }
 
 private IEnumerable<INotifyCompleted> SampleActions2(String country) {

   // Start a query for all customers in specified country, yield when async op completes.
   var op1 = _em1.Customers.Where(c => c.Country == country).ExecuteAsync();
   yield return op1;

   // Resume execution here when op1 completes.  Take a look at results from 1st query
   TraceFns.WriteLine("Customer count = " + op1.Results.Count().ToString());

   // Perform another async quuery for all employees.
   var op2 = _em1.Employees.ExecuteAsync();
   yield return op2;

   // Resume execution here when op2 completes.  See what it returned.
   TraceFns.WriteLine("Employee count = " + op2.Results.Count().ToString());
 }
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

Coroutine Class
Coroutine Members
Overload List

Send Feedback